package com.up.api.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: liuchengjun
 * @title: ThreadPoolUp
 * @projectName: 海联技术中台V2.0
 * @depart 软件事业部
 * @description:
 * @date: 2024/1/25/025 15:54 (星期四)
 * @since V2.0
 */
public class ThreadPoolUp {
    private ThreadPoolExecutor threadPoolExecutor;

    public static void main(String[] args) {
//        ThreadPoolUp threadPoolUp = new ThreadPoolUp();
//        ThreadPoolExecutor threadPoolExecutor1 = threadPoolUp.getThreadPoolExecutor();
//        CompletableFuture.runAsync(()->{
//            System.out.println("1"+Thread.currentThread().getName());
//
//        },threadPoolExecutor1);
//        CompletableFuture.runAsync(()->{
//            System.out.println("2"+Thread.currentThread().getName());
//        },threadPoolExecutor1);
//        threadPoolExecutor1.execute(()->System.out.println("3"+Thread.currentThread().getName()));
        ThreadPoolExecutor newThreadPoolExecutor = getNewThreadPoolExecutor();
        for (int i = 0; i < 100; i++) {
            newThreadPoolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    /**
     * 创建线程池通用方法
     * @return
     */
    public ThreadPoolExecutor getThreadPoolExecutor() {
        int coreSize = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor workerThreadPool = new ThreadPoolExecutor(coreSize, coreSize,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000), new ThreadFactory() {
            private final AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("workFlowDefinition-thread-%d", this.threadIndex.incrementAndGet()));
            }
        });
        workerThreadPool.allowCoreThreadTimeOut(true);
        return workerThreadPool;
    }
    /**
     * 创建线程池通用方法
     * @return
     */
    public static ThreadPoolExecutor getNewThreadPoolExecutor() {
        ThreadTaskQueue blockingQueue = new ThreadTaskQueue();
        ThreadPoolExecutor workerThreadPool = new ThreadPoolExecutor(2, 50,
                60, TimeUnit.SECONDS,
                blockingQueue, new ThreadFactory() {
            private final AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("workFlowDefinition-thread-%d", this.threadIndex.incrementAndGet()));
            }
        });
//        workerThreadPool.allowCoreThreadTimeOut(true);
        blockingQueue.setParent(workerThreadPool);
        return workerThreadPool;
    }

    public static ThreadPoolExecutor getNewThreadPoolExecutor2() {
        ThreadPoolExecutor workerThreadPool = new ThreadPoolExecutor(2, 4,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000), new ThreadFactory() {
            private final AtomicInteger threadIndex = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, String.format("workFlowDefinition-thread-%d", this.threadIndex.incrementAndGet()));
            }
        });
        return workerThreadPool;
    }


}
